***NBER Project
***Descriptive table












*Base Mensual del ENAHO 2019-2020 que solo tiene household income.
// gl enaho "$data_folder/ENAHO_monthly_hh_income.dta"

*NBER
gl nber "$data_folder/NBER_HPI_PANEL_with_bartick.dta"


*Base con ENDES del 2011-2019
// gl endes "$data_folder/endes_with_enaho_occup.dta"


*ENAHO 2019 couples only
// gl enaho2 "$data_folder/enahoData/daniel_data/enaho_2019_descriptive_couples.dta"

global covs2019 age age_partner hh_members sec_complete children_lockdown


// add weights 
use "$endesData/endes2019/REC0111.dta", clear
keep CASEID V005
tempfile endesWeights
sa `endesWeights'


//trim down the endes data 
// from 
// /HRI - MIMP/01_ProjectDevelopment/03 Covid Activities/04. Women Survey/03_Survey/Data Management/00 HFC/02_dofiles/07 endes code/3_cleaning_data.do
u "$data_folder/endes_with_enaho_occup", clear
keep if year == 2019 
merge 1:1 CASEID using `endesWeights', gen(endesWeightMerge)
keep any_our_any_last_12 any_psyco_last_12 any_our_physi_last_12  V005 year CASEID
tempfile endes
sa `endes'


********************************************************************************
*Panel A: Demographics
********************************************************************************
// from /HRI - MIMP/01_ProjectDevelopment/03 Covid Activities/04. Women Survey/03_Survey/Data Management/00 HFC/02_dofiles/02_Descriptive statistics/01 Enaho variables.do
use  "$data_folder/enaho_2019_descriptive_couples.dta", clear

gen enaho = 1
append using "$nber", gen (nber)

append using `endes', gen(endes) 


drop if age > 60 & nber == 1


//create some missing vars from nber data 
gen sec_complete = 1 if educ_woman >= 5 & nber == 1
replace sec_complete = 1 if educ_woman_grade_sec == 5 & nber == 1
replace sec_complete = 0 if sec_complete != 1 & educ_woman != . & nber == 1

gen sec_complete_enaho = (p301a >= 6) if !missing(p301a)



//replace to do test
replace age  = p208a 						if enaho == 1
replace age_partner = p208_partner		 	if enaho == 1
replace hh_members = mieperho			 	if enaho == 1
replace sec_complete = sec_complete_enaho	if enaho == 1
replace children_lockdown = n_children		if enaho == 1
replace income_hh2019 = hh_inc_couple_19 	if enaho == 1
replace income_hh2020 = hh_inc_couple_q2 	if enaho == 1
replace income_hh2021 = hh_inc_couple_q3 	if enaho == 1
replace income_hh = hh_income_couples 		if enaho == 1

replace dAny = any_our_any_last_12		if endes == 1
replace dPsy = any_psyco_last_12		if endes == 1 
replace dPhy = any_our_physi_last_12	if endes == 1





replace factor07 = 1 if nber == 1


//for timeinvariant covs, only use 2019 
foreach cov in age age_partner hh_members sec_complete children_lockdown dAny dPsy dPhy {
	gen `cov'_nm = `cov'
	replace `cov' = . if year != 2019 & nber ==1 
	replace `cov' = . if period != "2019" & nber ==0 
}

//for money make sure we are only using one time period
//this uses the fake year in nber data which is 2019 2020 and 2021
replace income_hh2019 = . if year != 2019 & nber == 1
replace  income_hh2020 = . if year != 2020 & nber == 1
replace income_hh2021 = . if year != 2021 & nber == 1



la var sec_complete			"% of women w/complete secondary"
la var age 					"Age (women)"
la var age_partner  		"Age (male partner)"
la var hh_members			"Household size"
la var children_lockdown	"Number of children"
la var income_hh2019		"Household income (2019)"
la var income_hh2020		"Household income (2020Q2)"
la var income_hh2021		"Household income (2020Q3)"
la var dPsy					"Psychological IPV"
la var dPhy					"Physical and sexual IPV"
la var dAny					"Any IPV"






replace nber = 0 if nber == . 


label define nber 1 "Phone Survey (2020)" 0 "Nationally Representative Survey"
la values nber nber





//
//
iebaltab $covs2019 ///
		income_hh2019  income_hh2020 income_hh2021 ///
		dPsy dPhy dAny ///
		,  grpvar(nber) rowvarlabels  tblnonote  ///
		save("$tex_folder/balance_table_unweighted.xlsx") replace

		

		

tostring id_cases, replace 

gen merge_id = id_cases
replace merge_id = hogar_id if merge_id == "."

replace merge_id = CASEID if endes == 1

bys nber: su income_hh

egen merge_id_num = group(merge_id)

tempfile descriptive_data
sa `descriptive_data'




replace year = 2019 if  enaho == 1 & period == "2019"
replace year = 2020 if  enaho == 1 & period == "2020Q2"
replace year = 2021 if  enaho == 1 & period == "2020Q3"

keep if nber == 1 | enaho == 1
keep $covs2019 income_hh2019   merge_id nber year merge_id_num


drop if missing(age, age_partner, hh_members ,sec_complete, children_lockdown,  income_hh2019) 

gen not_nber = nber != 1



sort merge_id year



label val nber


sa "$data_folder/data_for_weighting.dta", replace 


use `descriptive_data', clear


keep if nber == 1 | endes == 1
keep if year == 2019

keep dPhy dPsy dAny   merge_id nber year merge_id_num


drop if missing(dPhy, dPsy, dAny) 

gen not_nber = nber != 1



sort merge_id year
label val nber

sa "$data_folder/ipv_data_for_weighting.dta", replace 


 
//from ./getting_synth_weights.R
 
use `descriptive_data', clear

bys nber: su income_hh2019  income_hh2020 income_hh2021		

merge m:1 merge_id using  "$data_folder/synth_weights.dta", gen(weight_merge)
merge m:1 merge_id using  "$data_folder/synth_weights_ipv.dta", gen(weight_ipv_merge)

gen not_zero_w = unnormalized_weights != 0 if unnormalized_weights != . 

bys nber: su $covs2019 [aw=unnormalized_weights]




gen filled_in_w = unnormalized_weights
replace filled_in_w = 1 if unnormalized_weights == . 
 
gen filled_in_w_ipv = unnormalized_weights_ipv
replace filled_in_w_ipv = 1 if unnormalized_weights_ipv == . 
 

iebaltab $covs2019 ///
		income_hh2019  income_hh2020 income_hh2021 ///
		dPsy dPhy dAny ///
		[aw=filled_in_w] ///
		,  grpvar(nber) rowvarlabels  tblnonote  ///
		save("$tex_folder/balance_table_weighted.xlsx") replace

iebaltab $covs2019 ///
		income_hh2019  income_hh2020 income_hh2021 ///
		dPsy dPhy dAny ///
		[aw=filled_in_w_ipv] ///
		,  grpvar(nber) rowvarlabels  tblnonote  ///
		save("$tex_folder/balance_table_weighted_ipv.xlsx") replace
		
		
		
keep if nber == 1 
keep id_cases unnormalized_weights unnormalized_weights_ipv
duplicates drop 

destring id_cases, replace 

sa "$data_folder/nber_sample_with_weights.dta", replace 




